package demo.adapter;

import java.util.ArrayList;

import util.MLog;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import demo.bean.ChattingBean;
import demo.zandroid.R;

public class ChattingAdapter extends BaseAdapter {

	private Context context;
	private LayoutInflater mInflater;
	private ArrayList<ChattingBean> chattingBeans;

	private int VIEW_TYPE_COUNT;

	public ChattingAdapter(Context context,
			ArrayList<ChattingBean> chattingBeans) {
		this.context = context;
		this.chattingBeans = chattingBeans;
		mInflater = LayoutInflater.from(this.context);

		VIEW_TYPE_COUNT = chattingBeans.get(0).typeCount * 2;
		MLog.i(ChattingAdapter.class,
				"chattingBeans.size()=" + chattingBeans.size());
	}

	@Override
	public int getCount() {
		return chattingBeans.size();
	}

	@Override
	public Object getItem(int position) {
		return chattingBeans.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View v, ViewGroup parent) {
		int type = getItemViewType(position);
		switch (type) {
		case 0:
			v = getViewType0(position, v, parent);
			break;
		case 1:
			v = getViewType1(position, v, parent);
			break;
		case 2:
			v = getViewType2(position, v, parent);
			break;
		case 3:
			v = getViewType3(position, v, parent);
			break;
		}
		return v;
	}

	@Override
	public int getItemViewType(int position) {
		ChattingBean bean = chattingBeans.get(position);
		int i = bean.isMine ? bean.type * 2 + 1 : bean.type * 2;
		if (i < 0 || i >= VIEW_TYPE_COUNT) {
			MLog.e(getClass(), "getItemViewType(" + position
					+ ") IGNORE_ITEM_VIEW_TYPE");
			return IGNORE_ITEM_VIEW_TYPE;
		}
		return i;
	}

	@Override
	public int getViewTypeCount() {
		return VIEW_TYPE_COUNT;
	}

	public View getViewType0(int position, View v, ViewGroup parent) {
		ViewHolder0 holder = null;
		if (v == null) {
			v = mInflater.inflate(R.layout.chatting_item0_0, null);
			holder = new ViewHolder0();
			holder.imgUserIcon = (ImageView) v.findViewById(R.id.imgUserIcon);
			holder.textContent = (TextView) v.findViewById(R.id.textContent);
			v.setTag(R.layout.chatting_item0_0, holder);
		} else {
			holder = (ViewHolder0) v.getTag(R.layout.chatting_item0_0);
		}
		final ChattingBean bean = chattingBeans.get(position);
		holder.textContent.setText(bean.content);

		holder.imgUserIcon.setTag(bean);
		holder.imgUserIcon.setOnClickListener((OnClickListener) context);

		holder.textContent.setTag(bean);
		holder.textContent.setOnClickListener((OnClickListener) context);
		holder.textContent
				.setOnLongClickListener((OnLongClickListener) context);
		return v;
	}

	public View getViewType1(int position, View v, ViewGroup parent) {
		ViewHolder1 holder = null;
		if (v == null) {
			v = mInflater.inflate(R.layout.chatting_item0_1, null);
			holder = new ViewHolder1();
			holder.imgUserIcon = (ImageView) v.findViewById(R.id.imgUserIcon);
			holder.textContent = (TextView) v.findViewById(R.id.textContent);
			v.setTag(R.layout.chatting_item0_1, holder);
		} else {
			holder = (ViewHolder1) v.getTag(R.layout.chatting_item0_1);
		}
		final ChattingBean bean = chattingBeans.get(position);
		holder.textContent.setText(bean.content);

		holder.imgUserIcon.setTag(bean);
		holder.imgUserIcon.setOnClickListener((OnClickListener) context);

		holder.textContent.setTag(bean);
		holder.textContent.setOnClickListener((OnClickListener) context);
		holder.textContent
				.setOnLongClickListener((OnLongClickListener) context);
		return v;
	}

	public View getViewType2(int position, View v, ViewGroup parent) {
		ViewHolder2 holder = null;
		if (v == null) {
			v = mInflater.inflate(R.layout.chatting_item1_0, null);
			holder = new ViewHolder2();
			holder.imgUserIcon = (ImageView) v.findViewById(R.id.imgUserIcon);
			holder.imgContent = (ImageView) v.findViewById(R.id.imgContent);
			v.setTag(R.layout.chatting_item1_0, holder);
		} else {
			holder = (ViewHolder2) v.getTag(R.layout.chatting_item1_0);
		}
		final ChattingBean bean = chattingBeans.get(position);
		holder.imgUserIcon.setTag(bean);
		holder.imgUserIcon.setOnClickListener((OnClickListener) context);

		holder.imgContent.setTag(bean);
		holder.imgContent.setOnClickListener((OnClickListener) context);
		holder.imgContent.setOnLongClickListener((OnLongClickListener) context);
		return v;
	}

	public View getViewType3(int position, View v, ViewGroup parent) {
		ViewHolder3 holder = null;
		if (v == null) {
			v = mInflater.inflate(R.layout.chatting_item1_1, null);
			holder = new ViewHolder3();
			holder.imgUserIcon = (ImageView) v.findViewById(R.id.imgUserIcon);
			holder.imgContent = (ImageView) v.findViewById(R.id.imgContent);
			v.setTag(R.layout.chatting_item1_1, holder);
		} else {
			holder = (ViewHolder3) v.getTag(R.layout.chatting_item1_1);
		}
		final ChattingBean bean = chattingBeans.get(position);
		holder.imgUserIcon.setTag(bean);
		holder.imgUserIcon.setOnClickListener((OnClickListener) context);

		holder.imgContent.setTag(bean);
		holder.imgContent.setOnClickListener((OnClickListener) context);
		holder.imgContent.setOnLongClickListener((OnLongClickListener) context);
		return v;
	}

	private class ViewHolder0 {
		ImageView imgUserIcon;
		TextView textContent;
	}

	private class ViewHolder1 {
		ImageView imgUserIcon;
		TextView textContent;
	}

	private class ViewHolder2 {
		ImageView imgUserIcon;
		ImageView imgContent;
	}

	private class ViewHolder3 {
		ImageView imgUserIcon;
		ImageView imgContent;
	}

}
